#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Models/ShortRateModel.h>
#include <gen/QL/Models/TermStructureConsistentModel.h>
#pragma unmanaged 
#include <ql\models\shortrate\onefactormodels\blackkarasinski.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Models { namespace Shortrate { namespace Onefactormodels {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBlackKarasinski
	public ref class CBlackKarasinski : 
            public CTermStructureConsistentModel,
            public Cephei::QL::Models::Shortrate::Onefactormodels::IBlackKarasinski
	{
	protected: 
		boost::shared_ptr<QuantLib::BlackKarasinski>* _ppBlackKarasinski;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BlackKarasinski>* _phBlackKarasinski;
#endif
		Object^ _BlackKarasinskiOwner;     // reference to object that manages the storage for this object
	internal:
		CBlackKarasinski (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma);
        CBlackKarasinski (boost::shared_ptr<QuantLib::BlackKarasinski>& childNative, Object^ owner);
        CBlackKarasinski (QuantLib::BlackKarasinski& childNative, Object^ owner);
        CBlackKarasinski (CBlackKarasinski^ copy);
        CBlackKarasinski (System::Type^ t);
#ifdef STRUCT
        CBlackKarasinski (QuantLib::BlackKarasinski childNative);
#endif       
#ifdef HANDLE
		CBlackKarasinski (QuantLib::Handle<QuantLib::BlackKarasinski>& childNative, Object^ owner);
		CBlackKarasinski (QuantLib::Handle<QuantLib::BlackKarasinski> childNative);
#endif
		virtual ~CBlackKarasinski ();
		!CBlackKarasinski ();

	internal:
		QuantLib::BlackKarasinski& GetReference ();
		boost::shared_ptr<QuantLib::BlackKarasinski>& GetShared ();
		QuantLib::BlackKarasinski* GetPointer ();
        void SetBlackKarasinski (boost::shared_ptr<QuantLib::BlackKarasinski> native)
        {
            if (_ppBlackKarasinski != NULL)
                delete _ppBlackKarasinski;
            _ppBlackKarasinski = new boost::shared_ptr<QuantLib::BlackKarasinski> (native);
            SetTermStructureConsistentModel (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppBlackKarasinski));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BlackKarasinski>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Cephei::QL::ILattice^ Tree (Cephei::QL::ITimeGrid^ grid) ;
    private:
        Cephei::QL::Models::CShortRateModel^ _ShortRateModel;
    private:
        property CShortRateModel^ ShortRateModel
        {
            CShortRateModel^ get ()
            {
                if (_ShortRateModel == nullptr)
                {
                    _ShortRateModel = gcnew CShortRateModel (boost::dynamic_pointer_cast<QuantLib::ShortRateModel> (*_ppBlackKarasinski), this);
                }
                return _ShortRateModel;
            }
        }
    public:
        operator Cephei::QL::Models::IShortRateModel^ ()
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel);
        }
        operator Cephei::QL::Models::CShortRateModel^ ()
        {
            return ShortRateModel;
        }
        
    public:
        virtual Cephei::QL::Models::ICalibratedModel^ M1548_4593 (Cephei::IVector<Cephei::QL::Models::ICalibrationHelper^>^ instruments, Cephei::QL::Math::Optimization::IOptimizationMethod^ method, Cephei::QL::Math::Optimization::IEndCriteria^ endCriteria, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IConstraint^>^ constraint, Microsoft::FSharp::Core::FSharpOption<Cephei::IVector<Double>^>^ weights) = Cephei::QL::Models::ICalibratedModel::Calibrate
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Calibrate ( instruments,  method,  endCriteria,  constraint,  weights);
        }
        property Cephei::QL::Math::Optimization::IConstraint^ M1548_4595 
        {
            virtual Cephei::QL::Math::Optimization::IConstraint^ get () = Cephei::QL::Models::ICalibratedModel::Constraint::get
            {
                return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Constraint;
            }
        }
        property QL::Math::Optimization::EndCriteria::TypeEnum M1548_4596 
        {
            virtual QL::Math::Optimization::EndCriteria::TypeEnum get () = Cephei::QL::Models::ICalibratedModel::EndCriteria::get
            {
                return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->EndCriteria;
            }
        }
        virtual Cephei::QL::Models::ICalibratedModel^ M1548_4599 (Cephei::QL::Math::IArray^ p_params) = Cephei::QL::Models::ICalibratedModel::SetParams
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->SetParams ( p_params);
        }
        property Cephei::QL::Models::ICalibratedModel^ M1548_4600 
        {
            virtual Cephei::QL::Models::ICalibratedModel^ get () = Cephei::QL::Models::ICalibratedModel::Update::get
            {
                return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Update;
            }
        }
        virtual Double M1548_4601 (Cephei::QL::Math::IArray^ p_params, Cephei::IVector<Cephei::QL::Models::ICalibrationHelper^>^ instruments) = Cephei::QL::Models::ICalibratedModel::Value
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Value ( p_params,  instruments);
        }
        virtual Cephei::QL::ILattice^ M1548_Tree (Cephei::QL::ITimeGrid^ prm1)
        {
            return ((Cephei::QL::Models::Shortrate::Onefactormodels::IBlackKarasinski^)this)->Tree ( prm1);
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CBlackKarasinski_Factory : public System::MarshalByRefObject,  public IBlackKarasinski_Factory
	{
	public:
        virtual IBlackKarasinski^ Create (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma);
    };
   
/*Cephei*/ } /*QL*/ } /*Models*/ } /*Shortrate*/ } /*Onefactormodels */}
